﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Saxon.Api;
using WMM.Tapioca.Utilities;

namespace WMM.Tapioca.Formula.Aspects
{
    /// <summary>
    /// An explicit dimension rule is an aspect rule for an explicit dimension aspect 
    /// that is expressed by the <formula:explicitDimension> element. 
    /// </summary>
    public class ExplicitDimensionAspect : DimensionAspect
    {
        private XElement element;

        internal XElement Element
        {
            get { return element; }
            set { element = value; }
        }

        private string member;

        /// <returns> Identifies the QName of the domain member. </returns>
        internal string Member
        {
            get { return member; }
            set { member = value; }
        }

        private string omit;

        /// <returns> If an explicit dimension rule contains a <formula:omit> element then the dimension
        /// identified by the explicit dimension rule MUST NOT have a value for the output fact. . </returns>
        internal string Omit
        {
            get { return omit; }
            set { omit = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ExplicitDimensionAspect(XElement explicitDimensionAspect, FormulaXBRL formulaXbrl)
        {
            XNamespace formula = Constants.FORMULA;

            this.Element = explicitDimensionAspect;

            XElement aspect = explicitDimensionAspect.Parent;

            if (aspect.Attribute("source") != null)
            {
                this.Source = aspect.Attribute("source").Value;
            }

            if (explicitDimensionAspect.Attribute("source") != null)
            {
                this.Source = explicitDimensionAspect.Attribute("source").Value;
            }

            if (explicitDimensionAspect.Attribute("dimension") != null)
            {
                this.Dimension = explicitDimensionAspect.Attribute("dimension").Value;
            }

            XElement member = explicitDimensionAspect.Element(formula + "member");

            if (member != null)
            {
                XElement memberQname = member.Element(formula + "qname");

                if (memberQname != null)
                {
                    this.Member = "'" + memberQname.Value + "'";
                }

                XElement memberQnameExpression = member.Element(formula + "qnameExpression");

                if (memberQnameExpression != null)
                {
                    this.Member = memberQnameExpression.Value;
                }
            }

            XElement omit = explicitDimensionAspect.Element(formula + "omit");

            if (omit != null)
            {
                this.Omit = omit.Name.LocalName;
            }

            formulaXbrl.ExplicitDimensionAspectFormulas.Add(this);
        }


    }
}
